/********************************************************************************
Discrimination in Multi-Phase Systems: Evidence from Child Protection

Created on: 2/2/2024
Last Modified on: 2/18/2024

Description: This is the master do-file for our project. A replicator that runs 
this program will be able to replicate all figures and tables in the paper, except
for Table A12, which is created using Matlab.

Note that we have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

**************************
**(0) SETUP
**************************
clear
set more off
macro drop all
capture log close

*Set directories:
global clean 
global cleandata 
global tmpdata 
global rawdata 
global output 
global code 


*************************
**(1) INSTALL PACKAGES NEEDED FOR REPLICATION 
*************************
* Install ftools (remove program if it existed previously)
cap ado uninstall ftools
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")

* Install reghdfe 6.x
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")

* Install parallel, if using the parallel() option; don't install from SSC
cap ado uninstall parallel
net install parallel, from(https://raw.github.com/gvegayon/parallel/stable/) replace
mata mata mlib index

* Install gtools 
local github "https://raw.githubusercontent.com"
net install gtools, from(`github'/mcaceresb/stata-gtools/master/build/)

* Install wtmean 
ssc install _gwtmean, replace

* Install blind schemes needed for figures 
ssc install blindschemes, replace 


*************************
**(2) CLEAN INVESTIGATOR AND SCREENER MICRODATA 
*************************
*Screeners
cd "${code}Screener Sample"

do 1_sample_restrictions_screener_analysis_sample.do 

do 1_screener_analysis_sample_child_call_level.do 

*Investigators 
cd "${code}Investigator Sample"

do 1_clean_allegations.do

do 1_clean_case.do

do 1_clean_child_level.do 

do 1_clean_workers.do

do 1_clean_alleg_match.do

do 1_clean_living_arrangements.do

do 1_cw_child_case.do

do 1_clean_cw_pre2008.do

do 1_cw_analysis_sample.do

do 1_future_maltreatment.do 

do 1_match_ric.do 

do 1_full_history_school_nbhd.do

do 1_pre_variables.do

do 1_post_variables.do

do 1_gen_sample_inv_2017_2019.do 

do 1_gen_main_analysis_sample_investigators.do

do 1_gen_inv_covariates.do

do 1_gen_omitted_payoffs_num_children.do 

do 1_inv_analysis_sample_child_inv_level.do 


*************************
**(3) GENERATE SCREENER- AND INVESTIGATOR-LEVEL ANALYSIS SAMPLES 
*************************
*Screeners 
cd "${code}Screener Sample"

do 2_gen_screener_main_rates.do 

*Investigators
cd "${code}Investigator Sample"

do 2_gen_main_inv_rates.do 

do 2_gen_main_inv_rates_cov.do 

do 2_gen_main_inv_rates_het.do 

do 2_gen_main_inv_rates_later.do

do 2_gen_main_inv_rates_limited.do 

do 2_gen_main_inv_rates_shorter_times.do 

do 2_gen_main_inv_rates_unadj.do 

do 2_gen_main_inv_rates_infc.do 

*************************
**(4) GENERATE TABLES AND FIGURES IN THE PAPER 
*************************
cd "${code}"

do 3_tables.do 

do 3_figures.do 


*************************
**(5) GENERATE FIGURE A2 RHS AND VALUES UNDERLYING FIGURE 4 
*************************
cd "${code}/NCANDS"

do 4_ncands.do 

** Figure 4 can be created in make_maps.R
